﻿/*******************************************************************************
* KindEditor - WYSIWYG HTML Editor for Internet
* Copyright (C) 2006-2011 kindsoft.net
*
* @author Roddy <luolonghao@gmail.com>
* @site http://www.kindsoft.net/
* @licence http://www.kindsoft.net/license.php
*******************************************************************************/


//获取知识点分类
function Key_InitCatalog() {

    jQuery.ajax({
        type: "Get",
        async: false,
        url: "/EbookEdit/GetKeyPointCategoryList?ebookId=" + jQuery("#EbookId").val(),
        success: function (data) {

            jQuery("#key_chapterSelect").empty();

            jQuery("#key_chapterSelect").append('<option value="0">--请选择--</option>');

            for (var i = 0; i < data.length; i++) {

                jQuery("#key_KeyPointCategorySelect").append('<option value=' + data[i].Key + '>' + data[i].Name + '</option>');
            }
        }
    });
};


//获取知识点
function Key_GetKeyPoint(keyPointCategoryId) {

    if (keyPointCategoryId == "0") {
        return false;
    }
    jQuery.ajax({
        type: "Get",
        url: "/EbookEdit/GetKeyPointList?keyPointCategoryId=" + keyPointCategoryId + "&ebookId=" + jQuery("#EbookId").val(),
        success: function (data) {

            jQuery("#key_keyPointSelect").empty();

            jQuery("#key_keyPointSelect").append('<option value="0">--请选择--</option>');

            for (var i = 0; i < data.length; i++) {

                jQuery("#key_keyPointSelect").append('<option value=' + data[i].Key + '>' + data[i].Name + '</option>');
            }
        }
    });
}

KindEditor.plugin('anchorpoint', function (K) {

    var self = this,
        name = 'anchorpoint';

    self.clickToolbar(name, function () {
        //self.focus()  产生的BUG是弹出两个窗体
        //self.focus();

        var html = ['<div  style="padding:20px;">',
                    '<select id="key_KeyPointCategorySelect" style="width:150px" onchange="Key_GetKeyPoint(this.value)">',
                    '<option value="0">--请选择--</option>',
                    '</select>',
                    '</select>',
                    '<select id="key_keyPointSelect" style="width:150px;margin-left: 10px;">',
                    '<option value="0">--请选择--</option>',
                    '</select>',
                '</div>'].join('');

        var dialog = self.createDialog({
            name: name,
            width: 400,
            title: self.lang(name),
            body: html,
            yesBtn: {
                name: self.lang('yes'),
                click: function (e) {

                    var val = document.getElementById("key_keyPointSelect").value;

                    if (val == "0") {  //节锚点

                        alert('请选择知识点');
                        return;
                    }

                    var txt = document.getElementById("key_keyPointSelect").options[document.getElementById("key_keyPointSelect").selectedIndex].text;

                    action = "keypoint_";

                    key = document.getElementById("key_keyPointSelect").value;

                    var src = "/Content/anchor/knowbtn.gif";

                    self.insertHtml('<a href="javascript:void(0);" class="keypointanchor" title="知识点锚点：' + txt + '" target=' + key + ' name=' + (action + key) + '><img src="' + src + '"></a>').hideDialog().focus();
                    //self.insertHtml('<img class="keypointanchor" src="' + src + '" title="知识点锚点：' + txt + '" target=' + key + ' id="' + (action + key) + '" name="' + (action + key) + '">').hideDialog().focus();
                }
            }
        });

        Key_InitCatalog();
    });
});
